#include "../../mqserver/msg_queue.hpp"

#include <iostream>
#include <gtest/gtest.h>

NS_MsgQueue::MsgQueueManager emp("./meta/test");

class MsgQueueTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
    }

    virtual void TearDown() override
    {
        emp.removeAll();
    }
};

// 测试消息队列管理器的插入，查找，删除

TEST(msg_queue_test, insert_test)
{
    ASSERT_EQ(emp.size(), 1);
    std::string str_args = "k1=v1&k2=v2";
    emp.declareMsgQueue("msg_queue2", true, true, true, str_args);
    emp.declareMsgQueue("msg_queue3", true, true, true, str_args);
    emp.declareMsgQueue("msg_queue4", true, true, true, str_args);
    emp.declareMsgQueue("msg_queue5", true, true, true, str_args);
    ASSERT_EQ(emp.size(), 5);
}

TEST(msg_queue_test, delete_test)
{
    emp.deleteMsgQueue("msg_queue3");
    ASSERT_EQ(emp.size(), 4);
}

TEST(msg_queue_test, select_test)
{
    NS_MsgQueue::MsgQueue::Ptr e3 =  emp.selectOne("msg_queue3");
    ASSERT_EQ(e3, nullptr);

    NS_MsgQueue::MsgQueue::Ptr e2 =  emp.selectOne("msg_queue2");
    ASSERT_NE(e2, nullptr);
    ASSERT_EQ(e2->name, std::string("msg_queue2"));
    ASSERT_EQ(e2->exclusive, true);
    ASSERT_EQ(e2->durable, true);
    ASSERT_EQ(e2->auto_delete, true);
}

// 测试消息队列类型的其他参数为空时的错误检测 msg_queue.hpp:173 190
TEST(exchange_test, select_over_test)
{
    emp.declareMsgQueue("msg_queue1", true, true, true, "");
    NS_MsgQueue::MsgQueue::Ptr e1 = emp.selectOne("msg_queue1");
    ASSERT_NE(e1, nullptr);
}

// // 测试消息队列持久化类的插入、查找
// NS_MsgQueue::MsgQueuePersist per("./meta/test");

// class MsgQueueTest : public testing::Environment
// {
// public:
//     virtual void SetUp() override
//     {
//         per.createTable();
//     }

//     virtual void TearDown() override
//     {
//         // std::cout << "删除消息队列数据表" << std::endl;
//         // per.deleteTable();
//     }
// };

// TEST(msg_queue_test, insert_test)
// {
//     std::string str_args = "k1=v1&k2=v2";
//     NS_MsgQueue::MsgQueue::Ptr ptr = std::make_shared<NS_MsgQueue::MsgQueue>("msg_queue1", true, true, true);
//     ptr->setArgs(str_args);
//     per.insert(ptr);
// }

// TEST(msg_queue_test, select_test)
// {
//     NS_MsgQueue::MsgQueue::Ptr ptr = std::make_shared<NS_MsgQueue::MsgQueue>();
//     ptr = per.selectOne("msg_queue1");
//     ASSERT_EQ(ptr->name, std::string("msg_queue1"));
// }

// TEST(msg_queue_test, delete_test)
// {
//     per.erase("msg_queue1");
// }

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new MsgQueueTest());
    RUN_ALL_TESTS();
    return 0;
}